package com.brillilab.service.core.global.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.global.GlobalKitSpecMapper;
import com.brillilab.domain.enums.common.IsDeleteEnum;
import com.brillilab.domain.po.global.GlobalKit;
import com.brillilab.domain.po.global.GlobalKitSpec;
import com.brillilab.service.core.global.IGlobalKitService;
import com.brillilab.service.core.global.IGlobalKitSpecService;
import com.brillilab.service.core.system.IDictService;

@Service
public class GlobalKitSpecServiceImpl implements IGlobalKitSpecService {
	@Autowired
	private GlobalKitSpecMapper globalkitSpecMapper;
	
	@Autowired
	private IDictService dictService;
	
	@Autowired
	private IGlobalKitService globalKitService;
	
	@Override
	public GlobalKitSpec selectById(Long id) {
		return globalkitSpecMapper.selectById(id);
	}


	@Override
	public List<GlobalKitSpec> selectSpecGroup(Long kitId) {
		List<GlobalKitSpec> specList = new ArrayList<>();
		Map<BigDecimal, GlobalKitSpec> map = new HashMap<>();
		LambdaQueryWrapper<GlobalKitSpec> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(GlobalKitSpec::getKitId, kitId);
		wrapper.eq(GlobalKitSpec::getIsDelete, IsDeleteEnum.NO.getValue());
		List<GlobalKitSpec> list = globalkitSpecMapper.selectList(wrapper);
		list.forEach(kitSpec -> {
			//kit规格分组
			GlobalKitSpec spec = map.get(kitSpec.getKitSize());
			if(spec==null){
				map.put(kitSpec.getKitSize(), kitSpec);
				specList.add(kitSpec);
			}
		});
		return specList;
	}




	@Override
	public Map<Long,List<GlobalKitSpec>> selectSpecGroup(List<Long> kitIds) {
		Map<Long, List<GlobalKitSpec>> groupMap = new HashMap<>();
		//1、获取所有规格
		LambdaQueryWrapper<GlobalKitSpec> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(GlobalKitSpec::getIsDelete, IsDeleteEnum.NO.getValue());
		wrapper.in(GlobalKitSpec::getKitId, kitIds);
		List<GlobalKitSpec> list = globalkitSpecMapper.selectList(wrapper);
		if(list!=null && list.size()>0){
			//2、根据kitId分组规格
			Map<Long, List<GlobalKitSpec>> collectMap = list.stream().collect(Collectors.groupingBy(GlobalKitSpec::getKitId));
			collectMap.keySet().stream().forEach(kitId -> {
				List<GlobalKitSpec> specList = new ArrayList<>();				
				Map<BigDecimal, GlobalKitSpec> map = new HashMap<>();			
				collectMap.get(kitId).forEach(kitSpec -> {
					//3、根据kitSize分组规格
					GlobalKitSpec spec = map.get(kitSpec.getKitSize());
					if(spec==null){
						map.put(kitSpec.getKitSize(), kitSpec);
						specList.add(kitSpec);
					}
				});
				groupMap.put(kitId, specList);
			});
		}
		return groupMap;
	}
	
	@Override
	public List<GlobalKitSpec> selectList(Long kitId, BigDecimal kitSize) {
		LambdaQueryWrapper<GlobalKitSpec> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(GlobalKitSpec::getKitId, kitId);
		if (kitSize != null)
			wrapper.eq(GlobalKitSpec::getKitSize, kitSize);
		wrapper.eq(GlobalKitSpec::getIsDelete, IsDeleteEnum.NO.getValue());
		return globalkitSpecMapper.selectList(wrapper);
	}


	@Override
	public List<GlobalKitSpec> selectSpecList(Long kitId, Long reagentId) {
		LambdaQueryWrapper<GlobalKitSpec> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(GlobalKitSpec::getKitId, kitId);
		wrapper.eq(GlobalKitSpec::getReagentId, reagentId);
		wrapper.eq(GlobalKitSpec::getIsDelete, IsDeleteEnum.NO.getValue());
		return globalkitSpecMapper.selectList(wrapper);
	}
	
	@Override
	public String getSpecName(List<GlobalKitSpec> specList, Integer type) {
		String specName = "";
		if(specList!=null && specList.size()>0){
			Map<Integer, String> unitMap = dictService.getUnitMap();
			//组装规格信息
			if(type==0){//0-Kit规格
				int i = 1;
				GlobalKit kit = globalKitService.selectById(specList.get(0).getKitId());
				String unitName = unitMap.get(kit.getUnit());
				for (GlobalKitSpec kitSpec : specList) {
					if (kit.getKitType() == 0) {// 单成分
						specName += kitSpec.getSize().stripTrailingZeros().toPlainString() + unitName;
						if (kitSpec.getMultiple() > 1) specName += "*" + kitSpec.getMultiple();
					} else {// 多成分
						specName += kitSpec.getKitSize().stripTrailingZeros().toPlainString() + unitName;
					}
					if(i<specList.size()){
						specName += ",";
					}
					i++;
				}
			}else{//1-成分规格
				int i=1;
				for (GlobalKitSpec kitSpec : specList) {
					specName += kitSpec.getSize().stripTrailingZeros().toPlainString() + unitMap.get(kitSpec.getUnit());
					if (kitSpec.getMultiple() > 1)
						specName += "*" + kitSpec.getMultiple();
					if(i<specList.size()){
						specName += ",";
					}
					i++;
				}
			}
		}
		return specName;
	}
}
